package user.zc.config.system.template;

import user.zc.config.IConfig;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class LocalTemplateConfig extends TemplateConfig{

    Map<String,IConfig> map  = null;
    private static Set<String> defaultTemplateFiles =  new HashSet<String>(){{add("Controller");add("Dao");add("DaoExt");add("DTO");add("Entity");add("Mapper");add("MapperExt");add("Service");}};

    public LocalTemplateConfig() throws Exception {
        super();
    }

    public Map<String,IConfig> loadConfig() throws Exception {
        map = new HashMap<>();
        HashSet<String> templates = new HashSet<>(templateFiles());
        templates.addAll(defaultTemplateFiles);
        for (String file : defaultTemplateFiles) {
            map.put(file,new TemplateConfig(file));
        }
        return map;
    }

    private List<String> templateFiles(){
        File file =  new File(System.getProperty("user.home")+File.separator+ IConfig.FOLD+File.separator+"template");
        if(file.exists() && file.listFiles()!=null){
            return Arrays.asList(file.list()).stream().collect(Collectors.toList());
        }else{
            return new ArrayList<>();
        }
    }

    public void saveOrUpdate(){

    }

    public List<String> templates()throws Exception{
        if(map==null){
            loadConfig();
        }
        return map.keySet().stream().collect(Collectors.toList());
    }
}
